package com.firmboy.spring_axis2.config;

import org.apache.axis2.deployment.DeploymentConstants;
import org.apache.axis2.deployment.DeploymentEngine;
import org.apache.axis2.deployment.RepositoryListener;
import org.apache.axis2.deployment.repository.util.DeploymentFileData;
import org.apache.axis2.deployment.repository.util.WSInfo;

import java.io.File;

/**
 * @author firmboy
 * @create 2018-06-11 上午9:47
 **/
public class RepositoryListenerExport extends RepositoryListener {
    /**
     * This constructor takes two arguments, a folder name and a reference to Deployment Engine
     * First, it initializes the system, by loading all the modules in the /modules directory and
     * then creates a WSInfoList to store information about available modules and services.
     *
     * @param deploymentEngine reference to engine registry for updates
     * @param isClasspath      true if this RepositoryListener should scan the classpath for
     */
    public RepositoryListenerExport(DeploymentEngine deploymentEngine, boolean isClasspath) {
        super(deploymentEngine, isClasspath);
    }


    /**
     * Searches a given folder for aar files and adds them to a list in the WSInfolist class.
     * If sub folders found, those are also searched for services.
     * Ex : repository/services/foo/1.0.0/echo.aar
     *      repository/services/foo/1.0.1/echo.aar
     *      repository/services/echo.aar
     * @param root - directory from which we start searching for services
     */
    @Override
    protected void findServicesInDirectory(File root) {
        // flag to identify whether this is the services folder
        boolean servicesDir = true;
//        if (deploymentEngine.getServicesDir().getAbsolutePath().equals(root.getAbsolutePath())) {
//            servicesDir = true;
//        }
//        File[] files = root.listFiles();
//
//        if (files != null && files.length > 0) {
//            for (File file : files) {
//                if (isSourceControlDir(file)) {
//                    continue;
//                }
//                if (file.isDirectory()) {
//                    if (!(servicesDir && "lib".equalsIgnoreCase(file.getName())) &&
//                            !file.getName().startsWith(".")) {
//                        File servicesXML = new File(file, DeploymentConstants.SERVICES_XML);
//                        if (!servicesXML.exists()) {
//                            servicesXML =
//                                    new File(file, DeploymentConstants.SERVICES_XML.toLowerCase());
//                        }
//                        if (servicesXML.exists()) {
//                            addFileToDeploy(file, deploymentEngine.getServiceDeployer(),
//                                    WSInfo.TYPE_SERVICE);
//                        } else {
//                            findServicesInDirectory(file);
//                        }
//                    }
//                } else {
//                    if (DeploymentFileData.isServiceArchiveFile(file.getName())) {
//                        addFileToDeploy(file, deploymentEngine.getServiceDeployer(),
//                                WSInfo.TYPE_SERVICE);
//                    }
//                }
//            }
//        }
    }
}
